var $ = require('../../lib/jquery');
var util = require('./common/util');
var nav = require('./common/nav');
var _header = require('./common/header');
var template = require('../tpl/template');
var minHeight = require('./common/minHeight');
var tomap = require('./common/tomap');
minHeight.init();
var _nav = new nav();
var header = new _header();
var yiyuan  = {
  init: function () {
    this.fetch();
    this.keshi();
    this.bindevent()
  },
  tpl: '<p class="base" data-categoryid="{{categoryId}}"> <a href="#d{{index}}"  class="df" style="text-decoration: none;"> <span class="a"></span> <span class="b">{{categoryName}}</span> <span>><span> </a> </p>',
  ketpl: '<div class="block js_categoryid" id="d{{index}}" data-categoryid="{{categoryId}}"> <p class="title bb title1"><span class="name">{{categoryName}}</span></p> <div class="content bb" >{{span}}</div> </div>',
  spanTPl: '<span data-deptid="{{deptId}}" class="js_deptid" data-name="{{name}}">{{deptName}}</span>',
  fetch: function () {
    var _this = this;
    util.fetch('api/make/queryHospitalInfo',{
      hospital_id: util.getParam('id')
    }).then(function(res){
       sessionStorage.setItem('map', JSON.stringify(res.data));
      var tpl = template('yiyuan',{data: res.data});
      $('.js_yiyuan').html(tpl);
      _this.jingdu = res.data.jingdu;
      _this.weidu = res.data.weidu;
      _this.address = res.data.address;
      _this.name = res.data.name;
    })
  },
  keshi: function () {
    var _this = this;
    util.fetch('api/make/queryDepartment',{
      hospital_id: util.getParam('id'),
    }).then(function(res){
      var data = res.data ;
      var TPL = '',keTPL="",spanTPl="";
      (data || []).forEach(function(item,index){
        TPL += _this.tpl.replace('{{categoryId}}', item.categoryId).replace('{{index}}', index).replace('{{categoryName}}', item.categoryName);
        spanTPl="";
        item.items.forEach(function(value,key){
          spanTPl += _this.spanTPl.replace('{{deptId}}', value.deptId).replace('{{deptName}}', value.deptName).replace('{{name}}',value.deptName);
        })
        keTPL += _this.ketpl.replace('{{categoryName}}', item.categoryName).replace('{{span}}', spanTPl).replace('{{index}}', index).replace("{{categoryId}}", item.categoryId);
      }); 
      
      $('.js_left').html(TPL);
      $('.js_right').html(keTPL);
    })
  },
  bindevent: function () {
    var _this = this;
    $('body').on('click', '.js_deptid', function() {
      location.href = './yuyue.html?deptid=' + $(this).data().deptid + '&hospital_id=' + util.getParam('id') + '&categoryId=' + $(this).closest('.js_categoryid').data().categoryid + '&name='+ $(this).data().name
    })
    $('.js_yiyuan').on('click', '.right .btn2', function(){
      // 
      location.href = 'https://map.baidu.com/?latlng=' + _this.weidu + ',' + _this.jingdu + '&title=我的位置&content=' + _this.name + '&autoOpen=true&l'
    })
     $('.js_yiyuan').on('click', '.right .btn1', function () {
       location.href='./mapxq.html?type=1'
     })
    $('.js_back').on('click',function(){
        location.href ='./yygh.html'
    })
  },
}
yiyuan.init()
$('.js_yuyue').on('click', '.js_index', function () {
  location.href = './index.html'
})